from Crypto.Hash import SHA
from base64 import b64encode, b64decode
from Crypto.PublicKey import RSA
from Crypto.Signature import PKCS1_v1_5
import base64
import Crypto
from Crypto.Hash import MD5
import json
from Crypto.Signature import PKCS1_v1_5 as Signature_pkcs1_v1_5
from Crypto.Cipher import PKCS1_v1_5 as Cipher_pkcsl_v1_5

#
# strings = 'account=18680352162&device_id=99001100000824&password=123456&req_msg_id=9184499001100000824__msg_login&sn=99001100000824&version=2.3.0'
# with open(r'C:\Users\Administrator\Desktop\rsa_private_key.pem', 'rb') as f:
#     privatekey = RSA.importKey(f.read())
#     ciper = Cipher_pkcsl_v1_5.new(privatekey)
#     ciper_text = base64.b64encode(ciper.encrypt(strings))
#     print(ciper_text)
strings = {
    'merchantName': '1',
    'merchantShortName': '2',
    'outMerchantNo': '3',
    'agentNo': '4',
    'businessType': '5',
    'businessCode': '6',
    'businessName': '7',
    'businessExpire': '8',
    'businessFailure': '9',
}
print(json.dumps(strings, ensure_ascii=False))
privateKey = 'MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAwFwOoEpGIkzNySAxMuoCGdI5CT9NfOiLCT9DLPxgyQ+UWie' \
             '+gqktS8G25RnkAPht7qSOsncl1sa0XLQGO7qXWDwrRFQGLp5zRT4kWs/O3sa/Qo/gsb8fzdTZ1tJtC5lRm1qb6/gz331W7Dy+RQYJed1zR2C7t5EUjfWX/gzysh11vNNeF3Xs0Ruq1kWNVKECGqkB0D1tC8T0GwXpBVFPs+We8OgqKePok4BtLGx9GSFw6a60LzH53hsESWPI3GENmNdEiGTZh9EGFTr3aP5zhSO9pCoAaxOaTyO+vpDj9XRWvqWcWbMZryjMCmem918q6F2n3HOX2105HSL24qKgZQIDAQAB'

private_keyBytes = base64.b64decode(privateKey)
priKey = RSA.importKey(private_keyBytes)
signer = PKCS1_v1_5.new(priKey)
hash_obj = MD5.new(json.dumps(strings).encode('utf-8'))
signature = base64.b64encode(signer.sign(hash_obj))
print(signature)
